
import {ref,reactive,computed} from "vue"
import {defineStore} from "pinia"


// defineStore(modelName,options|function)
// modelName  模块名称(唯一id)
// options/function
//        options  传入options对象 -> 使用选项式语法
//        function  传入setup函数  => 组合式语法

// 注意: 选项式语法中  this->都指向当前store实例 -> 可以直接对于属性名/方法名,获取属性/计算属性/方法
export let useMdbStore = defineStore("mdb",function(){

    let a = ref(10);
    let b = ref(20);
    let zhang = reactive({name:"张三",age:18})


    // 计算属性
    let doubleA = computed(()=>{
        return a.value * 2;
    })

    let doubleB = computed(()=>{
        return b.value * 2;
    })

    let both = computed(()=>{
        return  a.value + b.value;
    })

    let doubleBoth = computed(()=>{
        return  doubleA.value + doubleB.value;
    })

    // 方法
    let addA = ()=>{
        a.value ++;
    }

    let addB = ()=>{
        b.value ++;
    }

    let  addA_Async = ()=>{
        return new Promise((resolve)=>{
            setTimeout(()=>{
                a.value++;
                resolve();
            },1000)
        })
    }

    let  addB_Async = ()=>{
        return new Promise((resolve)=>{
            setTimeout(()=>{
                b.value++;
                resolve();
            },1000)
        })
    }

    let  addBoth_Async = async ()=>{
        await addA_Async()
        await addB_Async()
    }





    return {
        a,
        b,
        zhang,
        doubleA,
        doubleB,
        both,
        doubleBoth,
        addA,
        addB,
        addA_Async,
        addB_Async,
        addBoth_Async
    }
})